<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\library\Ems;
use app\common\library\Sms;
use fast\Random;
use think\Config;
use think\Validate;
use think\Db;

/**
 * 剧情接口
 */
class Episode extends Api
{
    protected $noNeedLogin = ['getEpisodeInfo','getEpisodeChapter','getEpisodeBanner','getEpisodeHot','getEpisodeDetail','getEpisodeRemark','getEpisodeBarrage','getEpisodeBarrageDetail','getEpisodeBarrageRemark'];
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();
        if (!Config::get('fastadmin.usercenter')) {
            $this->error(__('User center already closed'));
        }
    }

     /**
     * 首页剧
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0",description="通讯状态，0为失败，1为成功")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功",description="返回内容")
     * @ApiReturnParams   (name="data",description="扩展数据返回")
     * @ApiReturnParams   (name="id", type="string", required=true, sample="返回成功",description="剧本id")
     * @ApiReturnParams   (name="name", type="string", required=true, sample="返回成功",description="剧本名称")
     * @ApiReturnParams   (name="image", type="string", required=true, sample="返回成功",description="剧本宣传图")
     * @ApiReturnParams   (name="file", type="string", required=true, sample="返回成功",description="剧本视频地址")
     */
    public function getEpisodeInfo()
    {
        $source = input("source");
        $where = array();
        if($this->auth->id){
            $user = DB::name("user")->where("id",$this->auth->id)->find();
            $lang_type = $user['lang_type'];
            $where["type"] = $lang_type;
            if($lang_type == 1){
                $where["id"] = $user['episode_script_id'];
            }else{
                $where["id"] = $user['eng_episode_script_id'];
            }
        }else{
            $lang_type = 1;
            $where["type"] = $lang_type;
        }
        
        $result = DB::name("episode_script")->where($where)->order("weigh desc")->find();
        if($this->auth->id){
            $episode_look = DB::name("episode_look")->where(array("episode_script_id"=>$result['id'],"user_id"=>$this->auth->id,"type"=>1))->order("id desc")->find();
            if($episode_look){
                $result['episode_info_id'] = $episode_look['episode_info_id'];
                $episode_info = DB::name("episode_info")->where(array("id"=>$episode_look['episode_info_id']))->order("id asc")->find();
                $result['file'] = $episode_info['file'];
            }else{
                $episode_info = DB::name("episode_info")->where(array("episode_script_id"=>$result['id'],"type"=>1))->order("id asc")->find();
                $result['episode_info_id'] = $episode_info['id'];
                $result['file'] = $episode_info['file'];
            }
        }else{
            $episode_info = DB::name("episode_info")->where(array("episode_script_id"=>$result['id'],"type"=>1))->order("id asc")->find();
            $result['episode_info_id'] = $episode_info['id'];
            $result['file'] = $episode_info['file'];
        }
        if($source == 1){
            if($episode_info['media_id']){
                $access_token = $this->get_access_token();
                $url = 'https://api.weixin.qq.com/wxa/sec/vod/getmedialink?access_token='.$access_token;
                $data = array(
                    "media_id" => $episode_info['media_id'],
                    "t" => time()+3600
                );
                //这里要把传递的参数给转成json，不然小程序接口会报数据类型错误。
                $res = json_decode($this->json_post($url,$data),true);
                if($res['errcode'] == '0'){
                    $media_info = $res['media_info'];
                    $result['file'] = $media_info['mp4_url'];
                }
            }
        }
        
        $this->success('查询成功', $result);
    }

     /**
     * 剧集列表
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0",description="通讯状态，0为失败，1为成功")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功",description="返回内容")
     * @ApiReturnParams   (name="data",description="扩展数据返回")
     * @ApiReturnParams   (name="id", type="string", required=true, sample="返回成功",description="章节id")
     * @ApiReturnParams   (name="name", type="string", required=true, sample="返回成功",description="章节名称")
     */
    public function getEpisodeChapter()
    {
        if($this->auth->id){
            $user = DB::name("user")->where("id",$this->auth->id)->find();
            $lang_type = $user['lang_type'];
            if($user['lang_type'] == 1){
                $episode_script_id = $user['episode_script_id'];
            }else{
                $episode_script_id = $user['eng_episode_script_id'];
            }
        }else{
            $lang_type = 1;
            $system = DB::name("system_info")->where("type",$lang_type)->find();
            $episode_script_id = $system['episode_script_id'];
        }
        
        $episode_script = DB::name("episode_script")->where("id",$episode_script_id)->find();
        
        $where = array(
            "episode_script_id" => $episode_script_id
        );
        $total_num = DB::name("episode_info")->where("episode_script_id",$episode_script_id)->count();
		$yes_num = DB::name("episode_look")->where(array("episode_script_id"=>$episode_script_id,"user_id"=>$this->auth->id))->group("episode_info_id")->count();
        $result = DB::name("episode_chapter")->where($where)->order("weigh desc")->field("id,name")->select();
        foreach($result as $key => $val){
            $result[$key]['info_list'] = $this->getInfo($val['id'],'',$episode_script['video_type']);
            $result[$key]['video_type'] = $episode_script['video_type'];
            $result[$key]['branch_image'] = $episode_script['branch_image'];
            $result[$key]['width'] = $episode_script['width'];
            $result[$key]['height'] = $episode_script['height'];
            $result[$key]['ratio'] = intval($yes_num/$total_num*10*10);
        }
        
        $res = $result;
        
        $this->success('查询成功', $res);
    }

    public function getInfo($episode_chapter_id,$episode_info_ids,$video_type){
        
        $where = "episode_chapter_id =".$episode_chapter_id." and type = 1";
        $info = DB::name("episode_info")->where($where)->select();
        $ids_arr = array();
        $videos_arr = array();
        foreach($info as $kk => $vv){
            $ids_arr[] = $vv['id'];
            if($vv['episode_video_ids']){
                $videos_arr[] = $vv['episode_video_ids'];
            }
        }
        $ids_str = implode(",",$ids_arr);
        $videos_str = implode(",",$videos_arr);
        $info_episode_look = DB::name("episode_look")->where("episode_info_id in (".$ids_str.") and user_id =".$this->auth->id)->group("episode_info_id")->select();
        $info_video_look = DB::name("episode_look")->where("episode_info_id in (".$videos_str.") and user_id =".$this->auth->id)->group("episode_info_id")->select();
        $all_look = array_merge($info_episode_look,$info_video_look);
        $new_info = array();
        if(empty($all_look)){
            foreach ($info as $item){
                $item['is_look'] = 3;
                $info[$item['id']] = $item;
                $new_info[$item['id']] = $item;
            }
        }else{
            $look_arr = array();
            $video_look_arr = array();
            foreach($info_episode_look as $k1 => $v1){
                $look_arr[$v1['episode_info_id']] = 1;
            }
            
			foreach($info_video_look as $k2 => $v2){
                $video_look_arr[$v2['episode_info_id']] = 1;
            }
            foreach ($info as $item){
                if(isset($look_arr[$item['id']])){
                    $item['is_look'] = 1;
                }else{
					if(isset($video_look_arr[$item['episode_video_ids']])){
						$item['is_look'] = 2;
					}else{
						$item['is_look'] = 3;
					}
                }
                
                $info[$item['id']] = $item;
                $new_info[$item['id']] = $item;
            }
        }
        // $new_info = array();
        // foreach ($info as $item){
        //     if($this->auth->id){
        //         if($item['episode_info_ids']){
        //             $info_episode_look = DB::name("episode_look")->where(array("user_id"=>$this->auth->id,"episode_info_id"=>$item['id']))->find();
        //             if($info_episode_look){
        //                 $item['is_look'] = 1;
        //             }else{
        //                 $pid_info_episode_look = DB::name("episode_look")->where("FIND_IN_SET('".$item['episode_video_ids']."', episode_info_id) > 0 and user_id =".$this->auth->id)->find();
        //                 if($pid_info_episode_look){
        //                     $item['is_look'] = 2;
        //                 }else{
        //                     $item['is_look'] = 3;
        //                 }
                        
        //             }
        //         }else{
        //             $item['is_look'] = 1;
        //         }
        //     }else{
        //         $item['is_look'] = 2;
        //     }
        //     // $item['xia_ids'] = '0';
        //     $new_info[$item['id']] = $item;
        // }
        foreach ($new_info as $key => $row){
            if($row['episode_video_ids'] == 0){
                // $new_info[$row['id']] = $row;
            }else{
                if(strpos($row['episode_video_ids'], ',') !== false){
                    $episode_video_ids = explode(",",$row['episode_video_ids']);
                    foreach ($episode_video_ids as $item){
                        if(isset($new_info[$item]['xia_ids'])){
                            $new_info[$item]['xia_ids'] .= ",".$row['id'];
                        }else{
                            $new_info[$item]['xia_ids'] = $row['id'];
                        }
                    }
                }else{
                    if(isset($new_info[$row['episode_video_ids']]['xia_ids'])){
                        $new_info[$row['episode_video_ids']]['xia_ids'] .= ",".$row['id'];
                    }else{
                        $new_info[$row['episode_video_ids']]['xia_ids'] = $row['id'];
                    }
                    
                }
            }
        }
        if(empty($info_episode_look)){
            foreach ($new_info as $kx => $vx){
				if(empty($vx['episode_video_ids'])){
					$new_info[$kx]['is_look'] = 1;
					$xia_look = DB::name("episode_info")->where("FIND_IN_SET('".$vx['id']."', episode_video_ids) > 0")->select();
					foreach($xia_look as $kw => $vx){
						$new_info[$vx['id']]['is_look'] = 2;
					}
				}
            }
        }
        $info_list = array();
        foreach ($new_info as $val){
            if(!isset($val['xia_ids'])){
                $val['xia_ids'] = 0;
            }
            $info_list[] = $val;
        }
        
        return $info_list;
        
        // $where = "episode_chapter_id =".$episode_chapter_id." and type = 1";
        // $new_info = DB::name("episode_info")->where($where)->select();
        // foreach ($new_info as $key => $val){
        //     if($this->auth->id){
        //         $info_episode_look = DB::name("episode_look")->where(array("user_id"=>$this->auth->id,"episode_info_id"=>$val['id']))->find();
        //         if($info_episode_look){
        //             $new_info[$key]['is_look'] = 1;
        //         }else{
        //             $new_info[$key]['is_look'] = 2;
        //         }
        //     }else{
        //         $new_info[$key]['is_look'] = 2;
        //     }
        //     $new_info[$key]['video_type'] = $video_type;
        // }
        // return $new_info;
        // if($episode_info_ids){
        //     $where = "episode_chapter_id =".$episode_chapter_id." and (FIND_IN_SET('".$episode_info_ids."', episode_video_ids) > 0) and type = 1";
        //     $new_where = "episode_chapter_id =".$episode_chapter_id." and (FIND_IN_SET('".$episode_info_ids."', episode_video_ids) > 0) and type = 1";
        // }else{
        //     $where = "episode_chapter_id =".$episode_chapter_id." and episode_video_ids = 0 and type = 1";
        //     $new_where = "episode_chapter_id =".$episode_chapter_id." and episode_video_ids = 0 and type = 1";
        // }
        
        // $info = DB::name("episode_info")->where($where)->field("id,name,image,thumbnail,type")->select();
        // $new_info = DB::name("episode_info")->where($new_where)->field("id,name,image,thumbnail,type")->select();
        // if($info){
        //     foreach($info as $key => $val){
        //         if($this->auth->id){
        //             $info_episode_look = DB::name("episode_look")->where(array("user_id"=>$this->auth->id,"episode_info_id"=>$val['id']))->find();
        //             if($info_episode_look){
        //                 $new_info[$key]['is_look'] = 1;
        //             }else{
        //                 $new_info[$key]['is_look'] = 2;
        //             }
        //         }else{
        //             $new_info[$key]['is_look'] = 2;
        //         }
        //         $info_list = $this->getInfo($episode_chapter_id,$val['id'],$video_type);
        //         if($info_list){
        //             foreach($info_list as $k => $v){
        //                 $info_list[$k]['video_type'] = $video_type;
        //                 if($this->auth->id){
        //                     $episode_look = DB::name("episode_look")->where(array("user_id"=>$this->auth->id,"episode_info_id"=>$v['id']))->find();
        //                     if($episode_look){
        //                         $info_list[$k]['is_look'] = 1;
        //                     }else{
        //                         $info_list[$k]['is_look'] = 2;
        //                     }
        //                 }else{
        //                     $info_list[$k]['is_look'] = 2;
        //                 }
        //             }
        //         }
                
        //         $info[$key]['info_list'] = $info_list;
        //         if($val['type'] == 1){
        //             $new_info[$key]['info_list'] = $info[$key]['info_list'];
        //         }
                
        //         $new_info[$key]['video_type'] = $video_type;
        //     }
        // }
        
        // return $new_info;
    }

    
     /**
     * 剧集详情
     * @param string $episode_info_id  剧情id
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0",description="通讯状态，0为失败，1为成功")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功",description="返回内容")
     * @ApiReturnParams   (name="data",description="扩展数据返回")
     * @ApiReturnParams   (name="id", type="string", required=true, sample="返回成功",description="剧情id")
     * @ApiReturnParams   (name="name", type="string", required=true, sample="返回成功",description="剧情名称")
     * @ApiReturnParams   (name="image", type="string", required=true, sample="返回成功",description="剧情图片")
     * @ApiReturnParams   (name="file", type="string", required=true, sample="返回成功",description="链接")
     * @ApiReturnParams   (name="type", type="string", required=true, sample="返回成功",description="类型:1=视频,2=普通选项,3=心理活动选项,4=进度条选项,5=滑动选项,6=晃动手机选项,7=监听声音选项")
     * @ApiReturnParams   (name="is_look", type="string", required=true, sample="返回成功",description="是否可看：1=可看，2=不可看")
     * @ApiReturnParams   (name="level_look", type="string", required=true, sample="返回成功",description="等级达标：1=达标,2=不达标")
     * @ApiReturnParams   (name="prop_look", type="string", required=true, sample="返回成功",description="道具达标：1=达标,2=不达标")
     * @ApiReturnParams   (name="producer_look", type="string", required=true, sample="返回成功",description="主演时长达标：1=达标,2=不达标")
     * @ApiReturnParams   (name="qualify_look", type="string", required=true, sample="返回成功",description="好感值达标：1=达标,2=不达标")
     */
    public function getEpisodeInfoDetail()
    {
        $episode_info_id = input("episode_info_id");
        $result = DB::name("episode_info")->where("id",$episode_info_id)->find();
        $episode_look = DB::name("episode_look")->where(array("user_id"=>$this->auth->id,"episode_info_id"=>$episode_info_id))->find();
        $source = input("source");
        if($source == 1){
            if($result['media_id']){
                $access_token = $this->get_access_token();
                $url = 'https://api.weixin.qq.com/wxa/sec/vod/getmedialink?access_token='.$access_token;
                $data = array(
                    "media_id" => $result['media_id'],
                    "t" => time()+3600
                );
                //这里要把传递的参数给转成json，不然小程序接口会报数据类型错误。
                $res = json_decode($this->json_post($url,$data),true);
                if($res['errcode'] == '0'){
                    $media_info = $res['media_info'];
                    $result['file'] = $media_info['mp4_url'];
                }
            }
        }
        $level_look = 1;
        $prop_look = 1;
        $producer_look = 1;
        $qualify_look = 1;

        if(empty($episode_look)){
            if(empty($result['system_level_id']) && empty($result['system_prop_id']) && empty($result['episode_producer_num']) && empty($result['cardiac_qualify_value'])){
                $result['is_look'] = 1;
            }else if(!empty($result['system_level_id'])){
                $user = DB::name("user")->where("id",$this->auth->id)->find();
                if(empty($user['level'])){
                    $level_look == 2;
                }else{
                    $system_level = DB::name("system_level")->where("id",$result['system_level_id'])->find();
                    $user_level = DB::name("system_level")->where("id",$user['level'])->find();
                    if($user_level['level'] >= $system_level['level']){
                        $level_look = 1;
                    }else{
                        $level_look = 2;
                    }
                }
            }else if(!empty($result['system_prop_id'])){
                $prop_look = 2;
            }else if(!empty($result['episode_producer_num'])){
                $usergood = DB::name("episode_usergood")->where(array("episode_character_id"=>$result["episode_character_id"],"user_id"=>$this->auth->id))->find();
                if($result['episode_producer_num'] > $usergood['video_num']){
                    $producer_look = 2;
                }else{
                    $producer_look = 1;
                }
            }else if(!empty($result['cardiac_qualify_value'])){
                $usergood = DB::name("episode_usergood")->where(array("episode_character_id"=>$result["episode_character_id"],"user_id"=>$this->auth->id))->find();
                if($result['cardiac_qualify_value'] > $usergood['num']){
                    $qualify_look = 2;
                }else{
                    $qualify_look = 1;
                }
            }  
            if($level_look == 1 && $prop_look == 1 && $producer_look == 1 && $qualify_look == 1){
                $result['is_look'] = 1;
            }else{
                $result['is_look'] = 2;
            }
        }else{
            $result['is_look'] = 1;
        }

        $where = "FIND_IN_SET('".$episode_info_id."', episode_info_ids) > 0";
        $result['second_episode_list'] = DB::name("episode_info")->where($where)->select();
        $result['level_look'] = $level_look;
        $result['prop_look'] = $prop_look;
        $result['producer_look'] = $producer_look;
        $result['qualify_look'] = $qualify_look;
        $this->success('查询成功', $result);
    }
    
    /**
     * 看剧扣费
     * @ApiMethod (POST)
     * @param string $episode_info_id  下级剧情id
     * @param string $user_bag_id  背包id
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0",description="通讯状态，0为失败，1为成功")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功",description="返回内容")
     * @ApiReturnParams   (name="data",description="扩展数据返回")
     * @ApiReturnParams   (name="is_look", type="string", required=true, sample="返回成功",description="是否可看：1=可看，2=不可看")
     */
    public function doLookEpisodeMoney()
    {
        $episode_info_id = input("episode_info_id");
        if(empty($episode_info_id)){
            $this->error("请选择所要观看的剧情");
        }
        $result = DB::name("episode_info")->where("id",$episode_info_id)->find();
        $episode_character = DB::name("episode_character")->where("id",$result['episode_character_id'])->find();
        $episode_look = DB::name("episode_look")->where(array("user_id"=>$this->auth->id,"episode_info_id"=>$episode_info_id))->find();
        $user = DB::name("user")->where("id",$this->auth->id)->find();
        
        $episode_pay = DB::name("episode_pay")->where(array("user_id"=>$this->auth->id,"episode_info_id"=>$episode_info_id))->find();
        if(empty($episode_look) && ($result['money'] > 0 || $result['system_prop_id'])){
            if(empty($episode_pay)){
                $episode_usergood = DB::name("episode_usergood")->where(array("episode_script_id"=>$result['episode_script_id'],"user_id"=>$this->auth->id,"episode_character_id"=>$result['episode_character_id']))->find();
               
                // if($episode_usergood['num'] <= 0){
                //     $money = $result['money'] *2;
                // }elseif($episode_usergood['num'] >0 && $episode_character['max_hot'] > $episode_usergood['num']){
                //     $money = $result['money']*(1-$episode_usergood['num']/$episode_character['max_hot']);
                // }else{
                //     $money = 0;
                // }
                if($result['system_prop_id']){
                    $user_bag_id = input("user_bag_id");
                    $system_prop = DB::name("system_prop")->where("id",$result['system_prop_id'])->find();
                    if($user_bag_id){
                        $user_bag = DB::name("user_bag")->where(array("id"=>$user_bag_id,"user_id"=>$this->auth->id,"status"=>1))->find();
                        if(empty($user_bag)){
                            return json(['code'=>888,'data'=>$system_prop,'msg'=>'请先购买道具']);
                        }  
                    }else{
                        $user_bag = DB::name("user_bag")->where(array("system_prop_id"=>$result['system_prop_id'],"user_id"=>$this->auth->id,"status"=>1))->find();
                        if(empty($user_bag)){
                            return json(['code'=>888,'data'=>$system_prop,'msg'=>'请先购买道具']);
                        }else{
                            $system_prop['user_bag_id'] = $user_bag['id'];
                            return json(['code'=>777,'data'=>$system_prop,'msg'=>'请先使用道具']);
                        }
                    }
                    
                }
                if($result['money'] > 0){
                    $money = $result['money'];
                    if($user['money'] < $money){
                        return json(['code'=>999,'data'=>'','msg'=>'嘶豆余额不足']);
                    }
                    DB::name("user")->where("id",$this->auth->id)->setDec("money",$money);
                    $pay_log = array(
                        "user_id" => $this->auth->id,
                        "episode_info_id" => $episode_info_id,
                        "money" => $money,
                        "create_time" => date("Y-m-d H:i:s")
                    );
                    // print_r($pay_log);exit;
                    DB::name("episode_pay")->insert($pay_log);
                    
                    $money_log = array(
                        "user_id" => $this->auth->id,
                        "money" => $money,
                        "before" => $user['money'],
                        "after" => $user['money']-$money,
                        "memo" => "观看剧情".$result['name']."扣除".$money."嘶豆",
                        "createtime" => time()
                    );
                    DB::name("user_money_log")->insert($money_log);
                }
                if($result['system_prop_id']){
                    $episode_pay = DB::name("episode_pay")->where(array("user_id" => $this->auth->id,"episode_info_id" => $episode_info_id))->find();
                    if(empty($episode_pay)){
                        $pay_log = array(
                            "user_id" => $this->auth->id,
                            "episode_info_id" => $episode_info_id,
                            "money" => 0,
                            "create_time" => date("Y-m-d H:i:s")
                        );
                        DB::name("episode_pay")->insert($pay_log);
                    }
                    //使用道具
                    DB::name("user_bag")->where(array("id"=>$user_bag_id,"user_id"=>$this->auth->id,"status"=>1))->update(array("status"=>2));
                }
                $this->success("观看成功");
            }else{
                $this->success('观看成功');
            }
            
        }else{
            $this->success('观看成功');
        }
    }

     /**
     * 看剧
     * @param string $episode_info_id  剧情id
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0",description="通讯状态，0为失败，1为成功")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功",description="返回内容")
     * @ApiReturnParams   (name="data",description="扩展数据返回")
     * @ApiReturnParams   (name="is_look", type="string", required=true, sample="返回成功",description="是否可看：1=可看，2=不可看")
     */
    public function doLookEpisodeInfo()
    {
        $episode_info_id = input("episode_info_id");
        if(empty($episode_info_id)){
            $this->error("请选择所要观看的剧情");
        }
        
        $result = DB::name("episode_info")->where("id",$episode_info_id)->find();
        $episode_look = DB::name("episode_look")->where(array("user_id"=>$this->auth->id,"episode_info_id"=>$episode_info_id))->find();
        if(empty($episode_look) && ($result['money'] > 0 || $result['system_prop_id'])){
            $episode_pay = DB::name("episode_pay")->where(array("user_id"=>$this->auth->id,"episode_info_id"=>$episode_info_id))->find();
            if(empty($episode_pay)){
                return json(['code'=>666,'data'=>'','msg'=>'请先付费']);
            }
        }
        
        $xia_result = DB::name("episode_info")->where("(FIND_IN_SET('".$episode_info_id."', episode_info_ids) > 0)",$episode_info_id)->select();
        $source = input("source")?input("source"):1;
        if($source == 1){
            if($result['media_id']){
                $access_token = $this->get_access_token();
                $url = 'https://api.weixin.qq.com/wxa/sec/vod/getmedialink?access_token='.$access_token;
                $data = array(
                    "media_id" => $result['media_id'],
                    "t" => time()+3600
                );
                //这里要把传递的参数给转成json，不然小程序接口会报数据类型错误。
                $res = json_decode($this->json_post($url,$data),true);


                //如果是40001，说明access_token过期失效了，必须强制获取最新的access_token
                if($res['errcode'] == '40001'){
                    $access_token = $this->get_latest_access_token();
                    $url = 'https://api.weixin.qq.com/wxa/sec/vod/getmedialink?access_token='.$access_token;
                    $data = array(
                        "media_id" => $result['media_id'],
                        "t" => time()+3600
                    );
                    //这里要把传递的参数给转成json，不然小程序接口会报数据类型错误。
                    $res = json_decode($this->json_post($url,$data),true);
                }



                
                if($res['errcode'] == '0'){
                    $media_info = $res['media_info'];
                    $result['file'] = $media_info['mp4_url'];
                }else {
                    file_put_contents("doLookEpisodeInfo.txt","errcode={$res['errcode']},errmsg={$res['errmsg']},access_token={$access_token},media_id={$data['media_id']},t={$data['t']}".PHP_EOL,FILE_APPEND);

                }
            }
            
            if($xia_result){
                foreach ($xia_result as $k => $val){
                    if($val['media_id']){
                        $access_token = $this->get_access_token();
                        $url = 'https://api.weixin.qq.com/wxa/sec/vod/getmedialink?access_token='.$access_token;
                        $data = array(
                            "media_id" => $val['media_id'],
                            "t" => time()+3600
                        );
                        //这里要把传递的参数给转成json，不然小程序接口会报数据类型错误。
                        $res = json_decode($this->json_post($url,$data),true);


                        //如果是40001，说明access_token过期失效了，必须强制获取最新的access_token
                        if($res['errcode'] == '40001'){
                            $access_token = $this->get_latest_access_token();
                            $url = 'https://api.weixin.qq.com/wxa/sec/vod/getmedialink?access_token='.$access_token;
                            $data = array(
                                "media_id" => $result['media_id'],
                                "t" => time()+3600
                            );
                            //这里要把传递的参数给转成json，不然小程序接口会报数据类型错误。
                            $res = json_decode($this->json_post($url,$data),true);
                        }



                        if($res['errcode'] == '0'){
                            $media_info = $res['media_info'];
                            $xia_result[$k]['file'] = $media_info['mp4_url'];
                        }else {
                            file_put_contents("doLookEpisodeInfo_xia_result.txt","errcode={$res['errcode']},errmsg={$res['errmsg']},access_token={$access_token},media_id={$data['media_id']},t={$data['t']}".PHP_EOL,FILE_APPEND);

                        }
                    }
                }
            }
        }
        $result['xia_result'] = $xia_result;
        // print_r($episode_look);exit;
        $level_look = 1;
        $prop_look = 1;
        $producer_look = 1;
        $qualify_look = 1;
        if(empty($episode_look)){
            $episode_usergood = DB::name("episode_usergood")->where(array("episode_script_id"=>$result['episode_script_id'],"user_id"=>$this->auth->id,"episode_character_id"=>$result['episode_character_id']))->find();
            if(empty($result['system_level_id']) && empty($result['system_prop_id']) && empty($result['episode_producer_num']) && empty($result['cardiac_qualify_value'])){
                // $json_arr = json_decode($result['json'],true);
                // DB::name("episode_usergood")->where(array("episode_script_id"=>$result['episode_script_id'],"user_id"=>$this->auth->id,"episode_character_id"=>$result['episode_character_id']))->setInc("video_num",$result['file_num']);
                // DB::name("episode_usergood")->where(array("episode_script_id"=>$result['episode_script_id'],"user_id"=>$this->auth->id,"episode_character_id"=>$result['episode_character_id']))->setInc("num",$result['cardiac_value']);
                // if($json_arr){
                //     foreach($json_arr as $key => $val){
                //         DB::name("episode_usergood")->where(array("episode_script_id"=>$result['episode_script_id'],"user_id"=>$this->auth->id,"episode_character_id"=>$val['id']))->setInc("num",$val['num']);
                //     }
                // }
                
                // $data = array(
                //     "user_id" => $this->auth->id,
                //     "episode_script_id" => $result['episode_script_id'],
                //     "episode_info_id" => $episode_info_id,
                //     "look_num" => 0,
                //     "type" => $result['type'],
                //     "create_time" => date("Y-m-d H:i:s")
                // );
                // DB::name("episode_look")->insert($data);
               
            }else if(!empty($result['system_level_id'])){
                // $user = DB::name("user")->where("id",$this->auth->id)->find();
                // if(empty($user['level'])){
                //     $level_look == 2;
                // }else{
                //     $system_level = DB::name("system_level")->where("id",$result['system_level_id'])->find();
                //     $user_level = DB::name("system_level")->where("id",$user['level'])->find();
                //     if($user_level['level'] >= $system_level['level']){
                //         $level_look = 1;
                //     }else{
                //         $level_look = 2;
                //     }
                // }
            }else if(!empty($result['system_prop_id'])){
                // $prop_look = 2;
            }else if(!empty($result['episode_producer_num'])){
                // $usergood = DB::name("episode_usergood")->where(array("episode_character_id"=>$result["episode_character_id"],"user_id"=>$this->auth->id))->find();
                // if($result['episode_producer_num'] > $usergood['video_num']){
                //     $producer_look = 2;
                // }else{
                //     $producer_look = 1;
                // }
            }else if(!empty($result['cardiac_qualify_value'])){
                // $usergood = DB::name("episode_usergood")->where(array("episode_character_id"=>$result["episode_character_id"],"user_id"=>$this->auth->id))->find();
                // if($result['cardiac_qualify_value'] > $usergood['num']){
                //     $qualify_look = 2;
                // }else{
                //     $qualify_look = 1;
                // }
            }  
            if($level_look == 1 && $prop_look == 1 && $producer_look == 1 && $qualify_look == 1){
                $json_arr = json_decode($result['json'],true);
                DB::name("episode_usergood")->where(array("episode_script_id"=>$result['episode_script_id'],"user_id"=>$this->auth->id,"episode_character_id"=>$result['episode_character_id']))->setInc("video_num",$result['file_num']);
                DB::name("episode_usergood")->where(array("episode_script_id"=>$result['episode_script_id'],"user_id"=>$this->auth->id,"episode_character_id"=>$result['episode_character_id']))->setInc("num",$result['cardiac_value']);
                
                //0329start
                $user_recharge_money = DB::name("user_recharge")->where(array("user_id"=>$this->auth->id,"status"=>2))->sum("money");
                $pow = pow($user_recharge_money, 1.0/3);
                $episode_usergood = DB::name("episode_usergood")->where(array("user_id"=>$this->auth->id,"episode_character_id"=>$result['episode_character_id']))->find();
                if($episode_usergood['num'] > 0){
                    $new_num = intval($pow*$episode_usergood['num']);
                }elseif($episode_usergood['num'] < 0){
                    $new_num = intval($pow);
                }else{
                    $new_num = 0;
                }
                DB::name("episode_usergood")->where("id",$episode_usergood['id'])->update(array("pow_num"=>$new_num));
                
                if($json_arr){
                    foreach($json_arr as $key => $val){
                        DB::name("episode_usergood")->where(array("episode_script_id"=>$result['episode_script_id'],"user_id"=>$this->auth->id,"episode_character_id"=>$val['id']))->setInc("num",$val['num']);
                        $episode_usergood = DB::name("episode_usergood")->where(array("user_id"=>$this->auth->id,"episode_character_id"=>$val['id']))->find();
                        if($episode_usergood['num'] > 0){
                            $new_num = intval($pow*$episode_usergood['num']);
                        }elseif($episode_usergood['num'] < 0){
                            $new_num = intval($pow);
                        }else{
                            $new_num = 0;
                        }
                        DB::name("episode_usergood")->where("id",$val['id'])->update(array("pow_num"=>$new_num));
                    }
                }
                
                $data = array(
                    "user_id" => $this->auth->id,
                    "episode_script_id" => $result['episode_script_id'],
                    "episode_info_id" => $episode_info_id,
                    "look_num" => 0,
                    "type" => $result['type'],
                    "create_time" => date("Y-m-d H:i:s")
                );
                DB::name("episode_look")->insert($data);
                
            }else{
                $result['is_look'] = 2;
            }
            
        }else{
            $data = array(
                "user_id" => $this->auth->id,
                "episode_script_id" => $result['episode_script_id'],
                "episode_info_id" => $episode_info_id,
                "look_num" => 0,
                "type" => $result['type'],
                "create_time" => date("Y-m-d H:i:s")
            );
            DB::name("episode_look")->insert($data);
            // DB::name("episode_usergood")->where(array("episode_script_id"=>$result['episode_script_id'],"user_id"=>$this->auth->id,"episode_character_id"=>$result['episode_character_id']))->setInc("num",$result['cardiac_value']);
        }
        $this->success('观看成功', $result);
    }

    /**
     * 轮播剧
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0",description="通讯状态，0为失败，1为成功")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功",description="返回内容")
     * @ApiReturnParams   (name="data",description="扩展数据返回")
     * @ApiReturnParams   (name="id", type="string", required=true, sample="返回成功",description="剧本id")
     * @ApiReturnParams   (name="name", type="string", required=true, sample="返回成功",description="剧本名称")
     * @ApiReturnParams   (name="image", type="string", required=true, sample="返回成功",description="剧本宣传图")
     * @ApiReturnParams   (name="file", type="string", required=true, sample="返回成功",description="剧本视频地址")
     */
    public function getEpisodeBanner()
    {
        if($this->auth->id){
            $user = DB::name("user")->where("id",$this->auth->id)->find();
            $lang_type = $user['lang_type'];
        }else{
            $lang_type = 1;
        }
        $where = array(
            "type" => $lang_type,
            "is_banner" => 2
        );
        $result = DB::name("episode_script")->where($where)->order("weigh desc")->field("id,name,image,file")->select();
        foreach ($result as $key => $val){
            if($this->auth->id){
                $episode_look = DB::name("episode_look")->where(array("episode_script_id"=>$val['id'],"user_id"=>$this->auth->id))->order("id desc")->find();
                if($episode_look){
                    $val['episode_info_id'] = $episode_look['episode_info_id'];
                }else{
                    $episode_info = DB::name("episode_info")->where(array("episode_script_id"=>$val['id']))->order("id asc")->find();
                    $val['episode_info_id'] = $episode_info['id'];
                }
            }else{
                $episode_info = DB::name("episode_info")->where(array("episode_script_id"=>$val['id']))->order("id asc")->find();
                $val['episode_info_id'] = $episode_info['id'];
            }
            $result[$key] = $val;
        }
        $this->success('查询成功', $result);
    }

    
    /**
     * 原热门剧
     * @param string $status  状态：1=筹拍中，2=筹拍成功
     * @param string $page  页码，默认为1
     * @param string $page_size  每页数量，默认15
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0",description="通讯状态，0为失败，1为成功")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功",description="返回内容")
     * @ApiReturnParams   (name="data",description="扩展数据返回")
     * @ApiReturnParams   (name="id", type="string", required=true, sample="返回成功",description="剧本id")
     * @ApiReturnParams   (name="name", type="string", required=true, sample="返回成功",description="剧本名称")
     * @ApiReturnParams   (name="image", type="string", required=true, sample="返回成功",description="剧本宣传图")
     * @ApiReturnParams   (name="file", type="string", required=true, sample="返回成功",description="剧本视频地址")
     * @ApiReturnParams   (name="score", type="string", required=true, sample="返回成功",description="评分")
     */
    public function getEpisodeHot()
    {
        if($this->auth->id){
            $user = DB::name("user")->where("id",$this->auth->id)->find();
            $lang_type = $user['lang_type'];
        }else{
            $lang_type = 1;
        }

        
        $where = array("type"=>$lang_type);
        $status = input("status");
        if($status == 1){
            $where['status'] = 2;
        }elseif($status == 2){
            $where['status'] = 1;
        }
        
        $page = input("page")?input("page"):1;
        $page_size = input("page_size")?input("page_size"):15;
        $list = DB::name("episode_script")->where($where)->order("yes_money desc")->page($page,$page_size)->select();
        $source = input("source");
        foreach ($list as $key => $val){
            if($this->auth->id){
                $episode_look = DB::name("episode_look")->where(array("episode_script_id"=>$val['id'],"user_id"=>$this->auth->id))->order("id desc")->find();
                if($episode_look){
                    $val['episode_info_id'] = $episode_look['episode_info_id'];
                }else{
                    $episode_info = DB::name("episode_info")->where(array("episode_script_id"=>$val['id']))->order("id asc")->find();
                    $val['episode_info_id'] = $episode_info['id'];
                }
            }else{
                $episode_info = DB::name("episode_info")->where(array("episode_script_id"=>$val['id']))->order("id asc")->find();
                $val['episode_info_id'] = $episode_info['id'];
            }
            if($source == 1){
                if($val['media_id']){
                    $access_token = $this->get_access_token();
                    $url = 'https://api.weixin.qq.com/wxa/sec/vod/getmedialink?access_token='.$access_token;
                    $data = array(
                        "media_id" => $val['media_id'],
                        "t" => time()+3600
                    );
                    //这里要把传递的参数给转成json，不然小程序接口会报数据类型错误。
                    $res = json_decode($this->json_post($url,$data),true);
                    if($res['errcode'] == '0'){
                        $media_info = $res['media_info'];
                        $val['file'] = $media_info['mp4_url'];
                    }
                }
            }
            $list[$key] = $val;
        }
        $result['list'] = $list;
        $result['total'] = DB::name("episode_script")->where($where)->count();
        $this->success('查询成功', $result);
    }
    
     /**
     * 剧本演员
     * @param string $episode_script_id  剧本id
     * @param string $status  状态：1=中选，2=选举中，3=落选
     * @param string $page  页码，默认为1
     * @param string $page_size  每页数量，默认15
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0",description="通讯状态，0为失败，1为成功")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功",description="返回内容")
     * @ApiReturnParams   (name="data",description="扩展数据返回")
     * @ApiReturnParams   (name="id", type="string", required=true, sample="返回成功",description="剧本id")
     * @ApiReturnParams   (name="name", type="string", required=true, sample="返回成功",description="剧本名称")
     * @ApiReturnParams   (name="image", type="string", required=true, sample="返回成功",description="剧本宣传图")
     * @ApiReturnParams   (name="file", type="string", required=true, sample="返回成功",description="剧本视频地址")
     * @ApiReturnParams   (name="score", type="string", required=true, sample="返回成功",description="评分")
     */
    public function getEpisodeCharacter()
    {
        if($this->auth->id){
            $user = DB::name("user")->where("id",$this->auth->id)->find();
            $lang_type = $user['lang_type'];
        }else{
            $lang_type = 1;
        }
        $episode_script_id = input("episode_script_id");
        $status = input("status")?input("status"):1;
        $where = array("episode_script_id"=>$episode_script_id,'status'=>$status);
        
        $page = input("page")?input("page"):1;
        $page_size = input("page_size")?input("page_size"):15;
        $list = DB::name("episode_character")->where($where)->order("weigh desc")->page($page,$page_size)->select();
        $result['list'] = $list;
        $result['total'] = DB::name("episode_character")->where($where)->count();
        $this->success('查询成功', $result);
    }
    
     /**
     * 演员心动值排行榜
     * @param string $episode_character_id  演员id
     * @param string $page  页码，默认为1
     * @param string $page_size  每页数量，默认15
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0",description="通讯状态，0为失败，1为成功")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功",description="返回内容")
     * @ApiReturnParams   (name="data",description="扩展数据返回")
     * @ApiReturnParams   (name="id", type="string", required=true, sample="返回成功",description="剧本id")
     * @ApiReturnParams   (name="name", type="string", required=true, sample="返回成功",description="剧本名称")
     * @ApiReturnParams   (name="image", type="string", required=true, sample="返回成功",description="剧本宣传图")
     * @ApiReturnParams   (name="file", type="string", required=true, sample="返回成功",description="剧本视频地址")
     * @ApiReturnParams   (name="score", type="string", required=true, sample="返回成功",description="评分")
     */
    public function getEpisodeCharacterBang()
    {
        if($this->auth->id){
            $user = DB::name("user")->where("id",$this->auth->id)->find();
            $lang_type = $user['lang_type'];
        }else{
            $lang_type = 1;
        }
        $episode_character_id = input("episode_character_id");
        $page = input("page")?input("page"):1;
        $page_size = input("page_size")?input("page_size"):15;
        
        $result = DB::name("episode_usergood")->where("episode_character_id",$episode_character_id)->order("pow_num desc,id asc")->page($page,$page_size)->select();
        foreach ($result as $key => $val){
            $usergood_user = DB::name("user")->where("id",$val['user_id'])->find();
            $result[$key]['nickname'] = $usergood_user['nickname'];
            $result[$key]['avatar'] = $usergood_user['avatar'];
        }

        $res['list'] = $result;
        $user_bang = DB::name('episode_usergood')->where(array("episode_character_id"=>$episode_character_id,"user_id"=>$this->auth->id))->find();
        $user_num = $user_bang['pow_num']?$user_bang['pow_num']:0;

        if($user_bang){
            $where = "episode_character_id =".$episode_character_id." and (pow_num >".$user_num." or (id <".$user_bang['id']." and pow_num =".$user_num."))";
//            $where = "episode_character_id =".$episode_character_id." and pow_num >".$user_num." and id <".$user_bang['id'];
        }else{
            $where = "episode_character_id =".$episode_character_id." and pow_num >".$user_num;
        }

        $res['user_bang']['user_num'] = $user_num;
        $bang_num = DB::name('episode_usergood')->where($where)->count();
        $res['user_bang']['bang_num'] = $bang_num+1;
        $res['user_bang']['nickname'] = $user['nickname'];
        $res['user_bang']['avatar'] = $user['avatar'];

        $shang = DB::name("episode_usergood")->where("episode_character_id =".$episode_character_id." and pow_num >".$user_num)->field("id,user_id,num,pow_num")->order("num asc")->find();
        $res['user_bang']['shang_cha'] = $shang['pow_num'] - $user_num;

        $this->success('查询成功', $res);
    }

     /**
     * 演员心动值前后五
     * @param string $episode_character_id  演员id
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0",description="通讯状态，0为失败，1为成功")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功",description="返回内容")
     * @ApiReturnParams   (name="data",description="扩展数据返回")
     * @ApiReturnParams   (name="id", type="string", required=true, sample="返回成功",description="剧本id")
     * @ApiReturnParams   (name="name", type="string", required=true, sample="返回成功",description="剧本名称")
     * @ApiReturnParams   (name="image", type="string", required=true, sample="返回成功",description="剧本宣传图")
     * @ApiReturnParams   (name="file", type="string", required=true, sample="返回成功",description="剧本视频地址")
     * @ApiReturnParams   (name="score", type="string", required=true, sample="返回成功",description="评分")
     */
    public function getEpisodeCharacterFive()
    {
        if($this->auth->id){
            $user = DB::name("user")->where("id",$this->auth->id)->find();
            $lang_type = $user['lang_type'];
        }else{
            $lang_type = 1;
        }
        $episode_character_id = input("episode_character_id");
        
        $user_bang = DB::name('episode_usergood')->where(array("episode_character_id"=>$episode_character_id,"user_id"=>$this->auth->id))->find();
        $user_num = $user_bang['num']?$user_bang['num']:0;

        if($user_bang){
            $where = "episode_character_id =".$episode_character_id." and num >".$user_num." and id <".$user_bang['id'];
        }else{
            $where = "episode_character_id =".$episode_character_id." and num >".$user_num;
        }

        $qian_five = DB::name("episode_usergood")->where("episode_character_id =".$episode_character_id." and num >=".$user_num)->order("num asc,id asc")->limit(5)->select();
        foreach ($qian_five as $key => $val){
            $usergood_user = DB::name("user")->where("id",$val['user_id'])->find();
            $qian_five[$key]['nickname'] = $usergood_user['nickname'];
            $qian_five[$key]['avatar'] = $usergood_user['avatar'];
        }

        $res['qian_five'] = $qian_five;

        
        $hou_five = DB::name("episode_usergood")->where("episode_character_id =".$episode_character_id." and num <=".$user_num)->order("num desc,id asc")->limit(5)->select();
        foreach ($hou_five as $key => $val){
            $usergood_user = DB::name("user")->where("id",$val['user_id'])->find();
            $hou_five[$key]['nickname'] = $usergood_user['nickname'];
            $hou_five[$key]['avatar'] = $usergood_user['avatar'];
        }

        $res['hou_five'] = $hou_five;
        

        $res['user_bang']['user_num'] = $user_num;
        $bang_num = DB::name('episode_usergood')->where($where)->count();
        $res['user_bang']['bang_num'] = $bang_num+1;
        $res['user_bang']['nickname'] = $user['nickname'];
        $res['user_bang']['avatar'] = $user['avatar'];
        $this->success('查询成功', $res);
    }
    
     /**
     * 嘶豆贡献排行榜
     * @param string $episode_script_id  剧本id
     * @param string $page  页码，默认为1
     * @param string $page_size  每页数量，默认15
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0",description="通讯状态，0为失败，1为成功")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功",description="返回内容")
     * @ApiReturnParams   (name="data",description="扩展数据返回")
     * @ApiReturnParams   (name="id", type="string", required=true, sample="返回成功",description="剧本id")
     * @ApiReturnParams   (name="name", type="string", required=true, sample="返回成功",description="剧本名称")
     * @ApiReturnParams   (name="image", type="string", required=true, sample="返回成功",description="剧本宣传图")
     * @ApiReturnParams   (name="file", type="string", required=true, sample="返回成功",description="剧本视频地址")
     * @ApiReturnParams   (name="score", type="string", required=true, sample="返回成功",description="评分")
     */
    public function getEpisodeVoteBang()
    {
        if($this->auth->id){
            $user = DB::name("user")->where("id",$this->auth->id)->find();
            $lang_type = $user['lang_type'];
        }else{
            $lang_type = 1;
        }
        $episode_script_id = input("episode_script_id");
        $page = input("page")?input("page"):1;
        $page_size = input("page_size")?input("page_size"):100;
        
        $result = DB::name("episode_vote")->where("episode_script_id",$episode_script_id)->group("user_id")->field("user_id,sum(money) as total_money")->order("total_money desc")->page($page,$page_size)->select();
        foreach ($result as $key => $val){
            $usergood_user = DB::name("user")->where("id",$val['user_id'])->find();
            $result[$key]['nickname'] = $usergood_user['nickname'];
            $result[$key]['avatar'] = $usergood_user['avatar'];
        }
        $res['list'] = $result;
        
        $user_bang = DB::name('episode_vote')->where(array("episode_script_id"=>$episode_script_id,"user_id"=>$this->auth->id))->field("sum(money) as total_money")->find();
        $user_num = $user_bang['total_money']?$user_bang['total_money']:0;
        $bang_num = DB::name("episode_vote")->where("episode_script_id",$episode_script_id)->group("user_id")->field("id,user_id,sum(money) as total_money,money")->having("total_money > ".$user_num)->order("total_money desc,id asc")
            ->count();
        $res['user_bang']['user_num'] = $user_num;
        $res['user_bang']['bang_num'] = $bang_num+1;
        $res['user_bang']['nickname'] = $user['nickname'];
        $res['user_bang']['avatar'] = $user['avatar'];

        $shang = DB::name("episode_vote")->where("episode_script_id",$episode_script_id)->group("user_id")->field("id,user_id,sum(money) as total_money,money")->having("total_money > ".$user_num)->order("total_money asc")->find();
        $res['user_bang']['shang_cha'] = $shang['total_money'] - $user_num;

        $this->success('查询成功', $res);
    }
    
    
     /**
     * 嘶豆贡献前后五
     * @param string $episode_script_id  剧本id
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0",description="通讯状态，0为失败，1为成功")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功",description="返回内容")
     * @ApiReturnParams   (name="data",description="扩展数据返回")
     * @ApiReturnParams   (name="id", type="string", required=true, sample="返回成功",description="剧本id")
     * @ApiReturnParams   (name="name", type="string", required=true, sample="返回成功",description="剧本名称")
     * @ApiReturnParams   (name="image", type="string", required=true, sample="返回成功",description="剧本宣传图")
     * @ApiReturnParams   (name="file", type="string", required=true, sample="返回成功",description="剧本视频地址")
     * @ApiReturnParams   (name="score", type="string", required=true, sample="返回成功",description="评分")
     */
    public function getEpisodeVoteFive()
    {
        if($this->auth->id){
            $user = DB::name("user")->where("id",$this->auth->id)->find();
            $lang_type = $user['lang_type'];
        }else{
            $lang_type = 1;
        }
        $episode_script_id = input("episode_script_id");
        $user_bang = DB::name('episode_vote')->where(array("episode_script_id"=>$episode_script_id,"user_id"=>$this->auth->id))->field("sum(money) as total_money")->find();
        $user_num = $user_bang['total_money']?$user_bang['total_money']:0;
        
        $qian_five = DB::name("episode_vote")->where("episode_script_id",$episode_script_id)->group("user_id")->field("id,user_id,sum(money) as total_money,money")->having("total_money >=".$user_num)->order("total_money asc,id asc")
            ->limit(5)->select();
        foreach ($qian_five as $key => $val){
            $usergood_user = DB::name("user")->where("id",$val['user_id'])->find();
            $qian_five[$key]['nickname'] = $usergood_user['nickname'];
            $qian_five[$key]['avatar'] = $usergood_user['avatar'];
        }
        $res['qian_five'] = $qian_five;
        
        $hou_five = DB::name("episode_vote")->where("episode_script_id",$episode_script_id)->group("user_id")->field("id,user_id,sum(money) as total_money,money")->having("total_money <=".$user_num)->order("total_money desc,id asc")
            ->limit(5)->select();
        foreach ($hou_five as $key => $val){
            $usergood_user = DB::name("user")->where("id",$val['user_id'])->find();
            $hou_five[$key]['nickname'] = $usergood_user['nickname'];
            $hou_five[$key]['avatar'] = $usergood_user['avatar'];
        }
        $res['hou_five'] = $hou_five;
        $bang_num = DB::name("episode_vote")->where("episode_script_id",$episode_script_id)->group("user_id")->field("id,user_id,sum(money) as total_money,money")->having("total_money > ".$user_num)->order("total_money desc,id asc")
            ->count();
        $res['user_bang']['user_num'] = $user_num;
        $res['user_bang']['bang_num'] = $bang_num+1;
        $res['user_bang']['nickname'] = $user['nickname'];
        $res['user_bang']['avatar'] = $user['avatar'];
        $this->success('查询成功', $res);
    }
    
     /**
     * 剧情演员心动值
     * @param string $episode_info_id  剧情id
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0",description="通讯状态，0为失败，1为成功")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功",description="返回内容")
     * @ApiReturnParams   (name="data",description="扩展数据返回")
     * @ApiReturnParams   (name="id", type="string", required=true, sample="返回成功",description="剧本id")
     * @ApiReturnParams   (name="name", type="string", required=true, sample="返回成功",description="剧本名称")
     * @ApiReturnParams   (name="image", type="string", required=true, sample="返回成功",description="剧本宣传图")
     * @ApiReturnParams   (name="file", type="string", required=true, sample="返回成功",description="剧本视频地址")
     * @ApiReturnParams   (name="score", type="string", required=true, sample="返回成功",description="评分")
     */
    public function getEpisodeInfoCharacter()
    {
        if($this->auth->id){
            $user = DB::name("user")->where("id",$this->auth->id)->find();
            $lang_type = $user['lang_type'];
        }else{
            $lang_type = 1;
        }
        $episode_info_id = input("episode_info_id");
        
        $episode_info = DB::name("episode_info")->where("id",$episode_info_id)->find();
        
        $character_arr = array();
        if($episode_info['episode_character_id']){
            $character_arr[$episode_info['episode_character_id']] = $episode_info['episode_character_id'];
        }
        if($episode_info['json']){
           $json_arr = json_decode($episode_info['json'],true);
           foreach ($json_arr as $item){
               $character_arr[$item['id']] = $item['id'];
           }
        }
        $result = array();
        if($character_arr){
            $character_str = implode(",",$character_arr);
            $result = DB::name("episode_character")->where("id in (".$character_str.")")->select();
            foreach ($result as $key => $val){
                $usergood = DB::name("episode_usergood")->where(array("episode_character_id"=>$val['id'],"user_id"=>$this->auth->id))->find();
                $result[$key]['usergood_num'] = $usergood['num'];
            }
        }
        $this->success('查询成功', $result);
        
    }

     /**
     * 演员投票
     * @ApiMethod (POST)
     * @param string $episode_character_id  演员id
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0",description="通讯状态，0为失败，1为成功")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功",description="返回内容")
     * @ApiReturnParams   (name="data",description="扩展数据返回")
     */
    public function doAddEpisodeCharacterVote()
    {
        // echo $this->auth->id;exit;
        // $this->success("瞎投个啥");
        $episode_character_id = input("episode_character_id");
        if(empty($episode_character_id)){
            $this->error("请选择需要投票的演员");
        }
        
        $episode_character = DB::name("episode_character")->where("id",$episode_character_id)->find();
        if(empty($episode_character)){
            $this->error("演员不存在");
        }
        if($episode_character['status'] != 2){
            $this->error("演员状态有误");
        }
        
        $episode_script = DB::name("episode_script")->where("id",$episode_character['episode_script_id'])->find();
        if(empty($episode_script)){
            $this->error("该剧有误");
        }
        if($episode_script['status'] != 2){
            $this->error("剧本有误");
        }
        if(strtotime($episode_script['end_time']) <= time()){
            $this->error("剧本筹拍已到期");
        }
        // print_r($episode_character);exit;
        // $episode_character_select = DB::name("episode_character")->where(array("episode_script_id"=>$episode_character['episode_script_id']))->order("num desc")->limit($episode_script['character_num'])->select();
        // print_r($episode_character_select);exit;
        
        $user_find = DB::name("user")->where("id",$this->auth->id)->find();
        if($user_find['money'] < $episode_script['dan_money']){
            return json(['code'=>999,'data'=>'','msg'=>'嘶豆余额不足']);
        }
        
        $vote_count = DB::name("episode_vote")->where("user_id =".$this->auth->id." and episode_character_id =".$episode_character_id." and create_time like '%".date("Y-m-d")."%'")->count();
        if($vote_count > 10){
            $this->error("每个用户对每个演员一天最多投票十次");
        }
        $data = array(
            "user_id" => $this->auth->id,
            "episode_script_id" => $episode_character['episode_script_id'],
            "episode_character_id" => $episode_character_id,
            "money" => $episode_script['dan_money'],
            "create_time" => date("Y-m-d H:i:s")
        );
        DB::name("episode_vote")->insert($data);
        
        DB::name("user")->where("id",$this->auth->id)->setDec("money",$episode_script['dan_money']);
        DB::name("episode_script")->where("id",$episode_character['episode_script_id'])->setInc("yes_money",$episode_script['dan_money']);
        DB::name("episode_character")->where("id",$episode_character_id)->setInc("num",$episode_script['dan_money']);
        
        if($episode_script['yes_money'] + $episode_script['dan_money'] >= $episode_script['money']){
            //筹拍完成了
            DB::name("episode_script")->where("id",$episode_character['episode_script_id'])->update(array("status"=>1));
            $episode_character_select = DB::name("episode_character")->where(array("episode_script_id"=>$episode_character['episode_script_id']))->order("num desc")->limit($episode_script['character_num'])->select();
            foreach ($episode_character_select as $key => $val){
                DB::name("episode_character")->where("id",$val['id'])->update(array("status"=>1));
            }
        }
        
        $this->success("投票成功");
    }
    
    
    /**
     * 剧本详情
     * @param string $episode_script_id  剧本id
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0",description="通讯状态，0为失败，1为成功")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功",description="返回内容")
     * @ApiReturnParams   (name="data",description="扩展数据返回")
     * @ApiReturnParams   (name="id", type="string", required=true, sample="返回成功",description="剧本id")
     * @ApiReturnParams   (name="name", type="string", required=true, sample="返回成功",description="剧本名称")
     * @ApiReturnParams   (name="image", type="string", required=true, sample="返回成功",description="剧本宣传图")
     * @ApiReturnParams   (name="file", type="string", required=true, sample="返回成功",description="剧本视频地址")
     * @ApiReturnParams   (name="score", type="string", required=true, sample="返回成功",description="评分")
     */
    public function getEpisodeDetail()
    {
        
        $episode_script_id = input("episode_script_id");
        if(empty($episode_script_id)){
            $this->error("请选择剧本");
        }
        $where = array("id"=>$episode_script_id);
        $result = DB::name("episode_script")->where($where)->find();
        if($this->auth->id){
            $episode_look = DB::name("episode_look")->where(array("episode_script_id"=>$result['id'],"user_id"=>$this->auth->id))->order("id desc")->find();
            if($episode_look){
                $result['episode_info_id'] = $episode_look['episode_info_id'];
            }else{
                $episode_info = DB::name("episode_info")->where(array("episode_script_id"=>$result['id']))->order("id asc")->find();
                $result['episode_info_id'] = $episode_info['id'];
            }
        }else{
            $episode_info = DB::name("episode_info")->where(array("episode_script_id"=>$result['id']))->order("id asc")->find();
            $result['episode_info_id'] = $episode_info['id'];
        }
        if($result['status'] == 2){
            $cha = strtotime($result['end_time']) - time();
            $result['end_time'] = intval($cha/24/3600);
            $result['vote_member_num'] = DB::name("episode_vote")->where("episode_script_id",$result['id'])->group("user_id")->count();
        }
        $this->success('查询成功', $result);
    }

    
    /**
     * 剧本评论
     * @param string $episode_script_id  剧本id
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0",description="通讯状态，0为失败，1为成功")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功",description="返回内容")
     * @ApiReturnParams   (name="data",description="扩展数据返回")
     * @ApiReturnParams   (name="id", type="string", required=true, sample="返回成功",description="评论id")
     * @ApiReturnParams   (name="start_num", type="string", required=true, sample="返回成功",description="评星数")
     * @ApiReturnParams   (name="score", type="string", required=true, sample="返回成功",description="评分")
     * @ApiReturnParams   (name="remark", type="string", required=true, sample="返回成功",description="评论内容")
     */
    public function getEpisodeRemark()
    {
        
        $episode_script_id = input("episode_script_id");
        if(empty($episode_script_id)){
            $this->error("请选择剧本");
        }
        $episode_remark = DB::name("episode_remark")->where("episode_script_id",$episode_script_id)->order("id desc")->select();
        foreach($episode_remark as $key => $val){
            $script_user = DB::name("user")->where("id",$val['user_id'])->find();
            $episode_remark[$key]['user_nickname'] = $script_user['nickname'];
            $episode_remark[$key]['user_avatar'] = $script_user['avatar'];
        }
        $result = $episode_remark;
        $this->success('查询成功', $result);
    }

    /**
     * 点评剧本
     * @ApiMethod (POST)
     * @param string $episode_script_id  剧本id
     * @param string $star_num  评星
     * @param string $remark    评论内容
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0",description="通讯状态，0为失败，1为成功")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功",description="返回内容")
     * @ApiReturnParams   (name="data",description="扩展数据返回")
     */
    public function doAddEpisodeRemark()
    {
        $episode_script_id = input("episode_script_id");
        if(empty($episode_script_id)){
            $this->error("请选择需要评论的剧本");
        }
        $episode_script = DB::name("episode_script")->where("id",$episode_script_id)->find();
        if(empty($episode_script)){
            $this->error("该剧有误");
        }
        $star_num = input("star_num");
        if(intval($star_num) > 5 || intval($star_num) < 0){
            $this->error("评星有误");
        }

        $remark = input("remark");
        if(empty($remark)){
            $this->error("评论内容有误");
        }

        $logic = new \app\api\filterWords();
        $remark = $logic->filter($remark,3);
        if (strpos($remark, "*") !== false) {
            $this->error("评论失败，评论内容含有违禁词");
        }

        $data = array(
            "user_id" => $this->auth->id,
            "episode_script_id" => $episode_script_id,
            "start_num" => $star_num,
            "score" => $star_num*2,
            "remark" => $remark,
            "create_time" => date("Y-m-d H:i:s")
        );
        DB::name("episode_remark")->insert($data);

        $where = array("episode_script_id"=>$episode_script_id);
        $new_score = DB::name("episode_remark")->where($where)->sum("score");
        $new_count = DB::name("episode_remark")->where($where)->count();

        $updata = array(
            "score" => $new_score/$new_count,
            "score_num" => $new_count
        );
        DB::name("episode_script")->where("id",$episode_script_id)->update($updata);

        $this->success("点评成功");
    }

    /**
     * 发送弹幕
     * @ApiMethod (POST)
     * @param string $episode_info_id  剧情id
     * @param string $time_val  弹幕位置，第多少秒
     * @param string $remark    弹幕内容
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0",description="通讯状态，0为失败，1为成功")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功",description="返回内容")
     * @ApiReturnParams   (name="data",description="扩展数据返回")
     */
    public function doAddEpisodeBarrage()
    {
        $episode_info_id = input("episode_info_id");
        if(empty($episode_info_id)){
            $this->error("请选择需要评论的剧情");
        }
        $episode_info = DB::name("episode_info")->where("id",$episode_info_id)->find();
        if(empty($episode_info)){
            $this->error("该剧情有误");
        }
        $time_val = input("time_val");
        if(intval($time_val) < 0){
            $this->error("弹幕位置有误");
        }
        $remark = input("remark");
        if(empty($remark)){
            $this->error("弹幕内容有误");
        }

        $logic = new \app\api\filterWords();
        $remark = $logic->filter($remark,3);
        if (strpos($remark, "*") !== false) {
            $this->error("发送弹幕失败，弹幕内容含有违禁词");
        }

        if($this->auth->id){
            $user = DB::name("user")->where("id",$this->auth->id)->find();
            $lang_type = $user['lang_type'];
        }else{
            $lang_type = 1;
        }
        
        $system_tesk = DB::name("system_task")->find();
        $user_task = Db::name('user_task')->where("type = 5 and user_id = ".$this->auth->id." and create_time like '%".date("Y-m-d")."%'")->find();
        if(empty($user_task)){
            $user = DB::name("user")->where("id",$this->auth->id)->find();
            //签到7天,从第一天开始
            $signin_num = $system_tesk['barrage_num'];
            $memo = "发送弹幕获得".$signin_num."嘶豆";
            //更新余额
            DB::name("user")->where("id",$this->auth->id)->setInc("money",$signin_num);
            //插入流水记录
            $money_log = array(
                "user_id" => $this->auth->id,
                "money" => $signin_num,
                "before" => $user['money'],
                "after" => $user['money']+$signin_num,
                "memo" => $memo,
                "createtime" => time()
            );
            DB::name("user_money_log")->insert($money_log);
            //插入签到记录
            $tesk_log = array(
                "user_id" => $this->auth->id,
                "num" => $signin_num,
                "type" => 5,
                "create_time" => date("Y-m-d H:i:s")
            );
            DB::name("user_task")->insert($tesk_log);
        }

        $data = array(
            "user_id" => $this->auth->id,
            "episode_script_id" => $episode_info['episode_script_id'],
            "episode_info_id" => $episode_info_id,
            "episode_barrage_id" => 0,
            "time_val" => $time_val,
            "like_num" => 0,
            "tread_num" => 0,
            "remark" => $remark,
            "lang_type" => $lang_type,
            "type" => 1,
            "create_time" => date("Y-m-d H:i:s")
        );
        DB::name("episode_barrage")->insert($data);
        $this->success("发送弹幕成功");
    }

    
    /**
     * 评论弹幕
     * @ApiMethod (POST)
     * @param string $episode_barrage_id  弹幕id
     * @param string $remark    评论内容
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0",description="通讯状态，0为失败，1为成功")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功",description="返回内容")
     * @ApiReturnParams   (name="data",description="扩展数据返回")
     */
    public function doAddEpisodeBarrageRemark()
    {
        $episode_barrage_id = input("episode_barrage_id");
        if(empty($episode_barrage_id)){
            $this->error("请选择需要评论的弹幕");
        }
        $episode_barrage = DB::name("episode_barrage")->where("id",$episode_barrage_id)->find();
        if(empty($episode_barrage)){
            $this->error("该弹幕有误");
        }
        $remark = input("remark");
        if(empty($remark)){
            $this->error("评论内容有误");
        }

        $logic = new \app\api\filterWords();
        $remark = $logic->filter($remark,3);
        if (strpos($remark, "*") !== false) {
            $this->error("评论失败，评论内容含有违禁词");
        }



        if($this->auth->id){
            $user = DB::name("user")->where("id",$this->auth->id)->find();
            $lang_type = $user['lang_type'];
        }else{
            $lang_type = 1;
        }

        $data = array(
            "user_id" => $this->auth->id,
            "episode_script_id" => $episode_barrage['episode_script_id'],
            "episode_info_id" => $episode_barrage['episode_info_id'],
            "episode_barrage_id" => $episode_barrage_id,
            "time_val" => 0,
            "like_num" => 0,
            "tread_num" => 0,
            "remark" => $remark,
            "lang_type" => $lang_type,
            "type" => 2,
            "create_time" => date("Y-m-d H:i:s")
        );
        DB::name("episode_barrage")->insert($data);
        $this->success("发送弹幕评论成功");
    }

    /**
     * 点赞/取消点赞弹幕/弹幕评论
     * @ApiMethod (POST)
     * @param string $episode_barrage_id  弹幕id
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0",description="通讯状态，0为失败，1为成功")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功",description="返回内容")
     * @ApiReturnParams   (name="data",description="扩展数据返回")
     */
    public function doLikeEpisodeBarrageRemark()
    {
        $episode_barrage_id = input("episode_barrage_id");
        if(empty($episode_barrage_id)){
            $this->error("请选择需要点赞的弹幕");
        }
        $episode_barrage = DB::name("episode_barrage")->where("id",$episode_barrage_id)->find();
        if(empty($episode_barrage)){
            $this->error("该弹幕有误");
        }

        $episode_barragelike = DB::name("episode_barragelike")->where(array("user_id"=>$this->auth->id,"episode_barrage_id"=>$episode_barrage_id))->find();
        if($episode_barragelike){
            DB::name("episode_barragelike")->where("id",$episode_barragelike['id'])->delete();
            DB::name("episode_barrage")->where("id",$episode_barrage_id)->setDec("like_num");
            $msg = "取消点赞成功";
        }else{
            $data = array(
                "user_id" => $this->auth->id,
                "episode_barrage_id" => $episode_barrage_id,
                "lang_type" => $episode_barrage['lang_type'],
                "create_time" => date("Y-m-d H:i:s")
            );
            DB::name("episode_barragelike")->insert($data);
            DB::name("episode_barrage")->where("id",$episode_barrage_id)->setInc("like_num");
            $msg = "点赞成功";
        }
        $this->success($msg);
    }

    
    /**
     * 踩一下弹幕
     * @ApiMethod (POST)
     * @param string $episode_barrage_id  弹幕id
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0",description="通讯状态，0为失败，1为成功")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功",description="返回内容")
     * @ApiReturnParams   (name="data",description="扩展数据返回")
     */
    public function doTreadEpisodeBarrageRemark()
    {
        $episode_barrage_id = input("episode_barrage_id");
        if(empty($episode_barrage_id)){
            $this->error("请选择需要踩一下的弹幕");
        }
        $episode_barrage = DB::name("episode_barrage")->where("id",$episode_barrage_id)->find();
        if(empty($episode_barrage)){
            $this->error("该弹幕有误");
        }

        $data = array(
            "user_id" => $this->auth->id,
            "episode_barrage_id" => $episode_barrage_id,
            "lang_type" => $episode_barrage['lang_type'],
            "create_time" => date("Y-m-d H:i:s")
        );
        DB::name("episode_barragetread")->insert($data);
        DB::name("episode_barrage")->where("id",$episode_barrage_id)->setInc("tread_num");
        $msg = "踩一下成功";
        $this->success($msg);
    }

    /**
     * 举报弹幕/弹幕评论
     * @ApiMethod (POST)
     * @param string $episode_barrage_id  弹幕id
     * @param string $type  举报类型
     * @param string $value  举报内容
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0",description="通讯状态，0为失败，1为成功")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功",description="返回内容")
     * @ApiReturnParams   (name="data",description="扩展数据返回")
     */
    public function doReportEpisodeBarrageRemark()
    {
        $episode_barrage_id = input("episode_barrage_id");
        if(empty($episode_barrage_id)){
            $this->error("请选择需要踩一下的弹幕");
        }
        $episode_barrage = DB::name("episode_barrage")->where("id",$episode_barrage_id)->find();
        if(empty($episode_barrage)){
            $this->error("该弹幕有误");
        }

        $type = input("type");
        if(empty("type")){
            $this->error("举报类型不能为空");
        }
        $value = input("value");
        if(empty($value)){
            $this->error("举报内容不能为空");
        }
        $data = array(
            "user_id" => $this->auth->id,
            "episode_barrage_id" => $episode_barrage_id,
            "type" => $type,
            "value" => $value,
            "lang_type" => $episode_barrage['lang_type'],
            "create_time" => date("Y-m-d H:i:s")
        );
        DB::name("episode_barragereport")->insert($data);
        $msg = "举报成功";
        $this->success($msg);
    }

    /**
     * 弹幕列表
     * @param string $episode_info_id  剧情id
     * @param string $time_start  开始秒数
     * @param string $time_end  结束秒数
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0",description="通讯状态，0为失败，1为成功")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功",description="返回内容")
     * @ApiReturnParams   (name="data",description="扩展数据返回")
     * @ApiReturnParams   (name="id", type="string", required=true, sample="返回成功",description="弹幕id")
     * @ApiReturnParams   (name="time_val", type="string", required=true, sample="返回成功",description="第几秒的弹幕")
     * @ApiReturnParams   (name="remark", type="string", required=true, sample="返回成功",description="弹幕内容")
     * @ApiReturnParams   (name="like_num", type="string", required=true, sample="返回成功",description="点赞数量")
     * @ApiReturnParams   (name="tread_num", type="string", required=true, sample="返回成功",description="踩数量")
     * @ApiReturnParams   (name="barrage_nickname", type="string", required=true, sample="返回成功",description="发送人姓名")
     * @ApiReturnParams   (name="barrage_avatar", type="string", required=true, sample="返回成功",description="发送人头像")
     */
    public function getEpisodeBarrage()
    {
        $episode_info_id = input("episode_info_id");
        if(empty($episode_info_id)){
            $this->error("请选择剧情");
        }
        
        $time_start = input("time_start")?input("time_start"):0;
        $time_end = input("time_end")?input("time_end"):20;

        $where = "episode_info_id = ".$episode_info_id." and time_val >=".$time_start." and time_val <=".$time_end." and type = 1 and is_show = 1";

        $list = DB::name("episode_barrage")->where($where)->order("id asc")->select();
        
        foreach($list as $key => $val){
            $user = DB::name("user")->where("id",$val['user_id'])->find();
            $list[$key]['barrage_nickname'] = $user['nickname'];
            $list[$key]['barrage_avatar'] = $user['avatar'];
        }
        $result['list'] = $list;
        $this->success('查询成功', $result);
    }


    /**
     * 弹幕详情
     * @param string $episode_barrage_id  弹幕id
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0",description="通讯状态，0为失败，1为成功")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功",description="返回内容")
     * @ApiReturnParams   (name="data",description="扩展数据返回")
     * @ApiReturnParams   (name="id", type="string", required=true, sample="返回成功",description="弹幕id")
     * @ApiReturnParams   (name="time_val", type="string", required=true, sample="返回成功",description="第几秒的弹幕")
     * @ApiReturnParams   (name="remark", type="string", required=true, sample="返回成功",description="弹幕内容")
     * @ApiReturnParams   (name="like_num", type="string", required=true, sample="返回成功",description="点赞数量")
     * @ApiReturnParams   (name="tread_num", type="string", required=true, sample="返回成功",description="踩数量")
     * @ApiReturnParams   (name="barrage_nickname", type="string", required=true, sample="返回成功",description="发送人姓名")
     * @ApiReturnParams   (name="barrage_avatar", type="string", required=true, sample="返回成功",description="发送人头像")
     */
    public function getEpisodeBarrageDetail()
    {
        
        $episode_barrage_id = input("episode_barrage_id");
        if(empty($episode_barrage_id)){
            $this->error("请选择弹幕");
        }
        $where = array("id"=>$episode_barrage_id);
        $result = DB::name("episode_barrage")->where($where)->find();
        $user = DB::name("user")->where("id",$result['user_id'])->find();
        $result['barrage_nickname'] = $user['nickname'];
        $result['barrage_avatar'] = $user['avatar'];
        $this->success('查询成功', $result);
    }

    /**
     * 弹幕评论
     * @param string $episode_barrage_id  弹幕id
     * @param string $page  页码
     * @param string $page_size  每页显示数量
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0",description="通讯状态，0为失败，1为成功")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功",description="返回内容")
     * @ApiReturnParams   (name="data",description="扩展数据返回")
     * @ApiReturnParams   (name="id", type="string", required=true, sample="返回成功",description="评论id")
     * @ApiReturnParams   (name="remark", type="string", required=true, sample="返回成功",description="内容")
     * @ApiReturnParams   (name="like_num", type="string", required=true, sample="返回成功",description="点赞数量")
     * @ApiReturnParams   (name="tread_num", type="string", required=true, sample="返回成功",description="踩数量")
     * @ApiReturnParams   (name="barrage_nickname", type="string", required=true, sample="返回成功",description="发送人姓名")
     * @ApiReturnParams   (name="barrage_avatar", type="string", required=true, sample="返回成功",description="发送人头像")
     */
    public function getEpisodeBarrageRemark()
    {
        
        $episode_barrage_id = input("episode_barrage_id");
        if(empty($episode_barrage_id)){
            $this->error("请选择弹幕");
        }
        $page = input("page")?input("page"):1;
        $page_size = input("page_size")?input("page_size"):15;
        $where = array("episode_barrage_id"=>$episode_barrage_id);
        $list = DB::name("episode_barrage")->where($where)->order("id desc")->page($page,$page_size)->select();
        foreach($list as $key => $val){
            $user = DB::name("user")->where("id",$val['user_id'])->find();
            $list[$key]['barrage_nickname'] = $user['nickname'];
            $list[$key]['barrage_avatar'] = $user['avatar'];
        }
        $result['list'] = $list;
        $result['total'] = DB::name("episode_barrage")->where($where)->count();
        $this->success('查询成功', $result);
    }
    
    /**
     * 上传嘉宾
     * @ApiMethod (POST)
     * @param string $episode_script_id  剧本id
     * @param string $name  嘉宾姓名
     * @param string $gender  性别:1=男,2=女
     * @param string $images  嘉宾图片
     * @param string $company  所属公司
     * @param string $remark  推荐宣言
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0",description="通讯状态，0为失败，1为成功")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功",description="返回内容")
     * @ApiReturnParams   (name="data",description="扩展数据返回")
     */
    public function doAddEpisodeGuest()
    {
        $user = DB::name('user')->where('id',$this->auth->id)->find();

        $episode_script_id = input("episode_script_id");
        if(empty($episode_script_id)){
            $this->error("请选择所要上传的剧本");
        }

        $name = input("name");
        if(empty($name)){
            $this->error("嘉宾姓名不能为空");
        }
        $gender = input("gender")?input("gender"):1;
        $images = input("images");
        if(empty($images)){
            $this->error("嘉宾照片不能为空");
        }
        $company = input("company");
        $remark = input("remark");
        $episode_script = DB::name("episode_script")->where("id",$episode_script_id)->find();
        if($user['money'] < $episode_script['dou_num']){
            return json(['code'=>999,'data'=>'','msg'=>'嘶豆余额不足']);
        }
        //扣除余额
        DB::name('user')->where('id',$this->auth->id)->setDec("money",$episode_script['dou_num']);
        //插入记录
        $money_log = array(
            "user_id" => $this->auth->id,
            "money" => $episode_script['dou_num'],
            "before" => $user['money'],
            "after" => $user['money']-$episode_script['dou_num'],
            "memo" => "上传嘉宾".$name."扣除".$episode_script['dou_num']."嘶豆",
            "createtime" => time()
        );
        DB::name("user_money_log")->insert($money_log);

        $data = array(
            "user_id" => $this->auth->id,
            "episode_script_id" => $episode_script_id,
            "name" => $name,
            "gender" => $gender,
            "images" => $images,
            "company" => $company,
            "remark" => $remark,
            "money" => $episode_script['dou_num'],
            "status" => 1,
            "create_time" => date("Y-m-d H:i:s")
        );
        DB::name("episode_guest")->insert($data);
        $msg = "上传成功，请等待后台审核";
        $this->success($msg);
    }


}
